package imoocvideodev.common.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * @Author: Redi
 * @Date: 2021/2/4 11:23
 * @Description: 文件上传util
 */
public class FileUtil {

    private static final String UPLOAD_PATH =  "D:/tmpCode/imooc-video-user-data";

    public static String uploadFile(MultipartFile file, String savePath){

        if (savePath!=null){
            savePath = UPLOAD_PATH + savePath ;
        }

        String realPath = savePath + "/" + file.getOriginalFilename();

        File dest = new File(realPath);

        if (!dest.getParentFile().exists()){
            dest.getParentFile().mkdirs();
        }

        try {
            file.transferTo(dest);
            return realPath;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        
    }

}
